# -*- coding: utf-8 -*-

import json
import sys

import requests
import os
from concurrent.futures import ThreadPoolExecutor

"""
1. 获取小说的所有章节
2. 获取每一章的内容

小说：https://dushu.baidu.com/pc/detail?gid=4306063500
目录：https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"4306063500"}
内容：https://dushu.baidu.com/api/pc/getChapterContent?data={"book_id":"4306063500","cid":"4306063500|1569782244","need_bookinfo":1}
"""

book_url = ''
catalog_url = 'https://dushu.baidu.com/api/pc/getCatalog'
content_url = 'https://dushu.baidu.com/api/pc/getChapterContent'

headers = {
    "Content-Type": "application/x-www-form-urlencoded",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36"
}


# 获取详情信息
def get_detail(url):
    resp = requests.get(url, headers=headers)
    dic = resp.json()
    return dic['data']['novel']

# 获取章节目录
def get_catalog(url):
    resp = requests.get(url, headers=headers)
    dic = resp.json()
    return dic['data']['novel']


# 获取章节内容
def get_chapter_content(book_id, c_id, book_name):
    data = {
        "book_id": book_id,
        "cid": f"{book_id}|{c_id}",
        "need_bookinfo": 1
    }
    data = json.dumps(data)
    download_url = f"https://dushu.baidu.com/api/pc/getChapterContent?data={data}"
    resp = requests.get(download_url)
    result = resp.json()
    novel = result['data']['novel']
    chapter_title = novel['chapter_title']
    with open(f"/data/novel/{book_name}/{c_id} {chapter_title}.txt", mode='w', encoding='utf-8') as f:
        f.write(novel['content'])


# 异步下载
def aio_download(book_detail, catalog_info):
    book_id = catalog_info['book_id']
    # for catalog in catalog_info['items']:
    #     get_chapter_content(book_id, catalog['cid'])
    with ThreadPoolExecutor(10) as pool:
        for catalog in catalog_info['items']:
            pool.submit(get_chapter_content, book_id=book_id, c_id=catalog['cid'], book_name=book_detail['book_name'])


# 创建文件夹
def mkdir(path):
    dir_arr = path.split("/")
    cur_dir = "/"
    for sub_dir in dir_arr:
        if sub_dir:
            cur_dir += sub_dir + "/"
            try:
                os.chdir(cur_dir)
            except:
                os.mkdir(cur_dir)


if __name__ == '__main__':
    # book_id = "4306063500" # 西游记
    # book_id = "4305547728" # 剑来
    # book_id = "4356290733" # 万相之王
    book_id = sys.argv[1]
    print("book_id:" + book_id)
    # 1. 获取小说详情
    book_detail = get_detail('https://dushu.baidu.com/api/pc/getDetail?data={"book_id":"' + book_id+ '"}')
    print("book_name:" + book_detail['book_name'])
    # 2. 获取小说目录
    catalog_info = get_catalog('https://dushu.baidu.com/api/pc/getCatalog?data={"book_id":"' + book_id + '"}')
    # print(catalog_info)
    # 3. 获取章节内容
    book_name=book_detail['book_name']
    mkdir("/data/novel/" + book_name) # 创建文件夹
    aio_download(book_detail, catalog_info)
    print("Done!")
